#include<iostream>
using namespace std;

//1.字符串转整型
class Solution1 {
public:
    int myAtoi(string str)
    {
        int res = 0;
        int bndry = INT_MAX / 10;
        int i = 0;
        int sign = 1;
        int length = str.size();
        if (length == 0)
            return 0;
        while (str[i] == ' ')
        {
            if (++i == length)
                return 0;
        }
        if (str[i] == '-')
            sign = -1;
        if (str[i] == '-' || str[i] == '+')
            i++;
        for (int j = i; j < length; j++)
        {
            if (str[j] < '0' || str[j] > '9')
                break;
            if (res > bndry || res == bndry && str[j] > '7')
                return sign == 1 ? INT_MAX : INT_MIN;
            res = res * 10 + (str[j] - '0');
        }
        return sign * res;
    }
};
//2.字符串相加输出字符串
class Solution2
{
public:
    string addStrings(string num1, string num2) 
    {
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        int add = 0;
        string tmp = "";
        while (i >= 0 || j >= 0 || add != 0)
        {
            int x = i >= 0 ? num1[i] - '0' : 0;
            int y = j >= 0 ? num2[j] - '0' : 0;
            int result = x + y + add;
            tmp.push_back('0' + result % 10);
            add = result / 10;
            i -= 1;
            j -= 1;
        }
        reverse(tmp.begin(), tmp.end());
        return tmp;
    }
};

int main()
{
    Solution1 s1;
    cout<<s1.myAtoi("12345")<<endl;
    Solution2 s2;
    cout << s2.addStrings("321", "456")<<endl;
	return 0;
}